package seismosurfer.layer;

import java.awt.event.ActionListener;
import java.util.Properties;

import seismosurfer.http.LayerUtil;

import com.bbn.openmap.layer.shape.ShapeLayer;
import com.bbn.openmap.layer.shape.SpatialIndex;
import com.bbn.openmap.omGraphics.DrawingAttributes;
import com.bbn.openmap.util.PropUtils;

/**
 * An extension to the ShapeLayer used for data
 * that are stored in shape files (e.g. faults, plates).
 *
 */
public class SeismoShapeLayer extends ShapeLayer implements ActionListener {

    private static final long serialVersionUID = -6784751632119064279L;

    public final static String shapePathProperty = "shapePath";

    protected boolean fileRead = false;

    protected String shapePath;

    public SeismoShapeLayer() {
        super();
    }

    public SeismoShapeLayer(String shapeFileName) {
        super(shapeFileName);
    }

    public void setVisible(boolean show) {

        // Read Shape file from server

        if (!fileRead && show) {
            String localFilePath = LayerUtil.loadShapeFile(shapePath);
            readShapeFile(localFilePath);

            fileRead = true;
        }

        super.setVisible(show);
    }
    
    /**
     * Calls SpatialIndex.locateAndSetShapeData to read
     * the shape file and its index file.
     * 
     * @param path the file path of the shape file
     */
    protected void readShapeFile(String path) {

        String shapeFileName = path + ".shp";

        String spatialIndexFileName = path + ".ssx";

        spatialIndex = SpatialIndex.locateAndSetShapeData(shapeFileName,
                spatialIndexFileName);

    }

    public void setProperties(String prefix, Properties props) {

        super.setProperties(prefix, props);

        String realPrefix = PropUtils.getScopedPropertyPrefix(this);

        shapePath = props.getProperty(realPrefix + shapePathProperty);
        System.out.println("SeismoShapeLayer shapePath :" + shapePath);

        drawingAttributes = new DrawingAttributes(prefix, props);

        shadowX = PropUtils.intFromProperties(props, realPrefix
                + shadowXProperty, 0);
        shadowY = PropUtils.intFromProperties(props, realPrefix
                + shadowYProperty, 0);
    }    

}
